//
//  ViewController.m
//  Rac
//
//  Created by 崔博 on 2017/12/28.
//  Copyright © 2017年 崔博. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#include <ReactiveObjC/RACReturnSignal.h>
#import "BBViewModel.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (nonatomic, strong) BBViewModel * bbViewModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    UIButton *btn = [UIButton buttonWithType:0];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:btn];
    
    
    btn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
        return nil;
    }];
    
    [[btn rac_signalForControlEvents:UIControlEventAllEvents] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.bbViewModel.btnCommad execute:@""];
    }];
    [[self.bbViewModel.btnCommad execute:@""] subscribeNext:^(id  _Nullable x) {
        NSLog(@"x");
    }];
//    
//    RAC(self.titleLabel,text) = self.textField.rac_textSignal;
//    
//    RACSignal * sigB = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
//        return nil;
//    }];
//    [sigB subscribeError:^(NSError * _Nullable error) {
//        NSLog(@"我是B");
//    }];
//    RACSignal * conSig = [sigB concat:sigA];
//    [conSig subscribeNext:^(id  _Nullable x) {
//        NSLog(@"Con%@",x);
//    } error:^(NSError * _Nullable error) {
//        NSLog(@"%@",error);
//    }];
    
    RACSignal * sig1= [[self rac_signalForSelector:@selector(viewWillAppear:)] mapReplace:@"我是出现"];
    RACSignal * sig2 = [[self rac_signalForSelector:@selector(viewWillDisappear:)] mapReplace:@"我是销毁"];
//    RACSignal * sig3 = [sig1 merge:sig2];
//    RACSignal * sig3 = [sig1 zipWith:sig2];

    // zip
    RACSignal * sig3 = [sig1 combineLatestWith:sig2];
    [sig3 subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    
    
}
- (void)RACCommandd{
    
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"%@",input);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"执行完命令产生的数据"];
            // 发送完毕
            [subscriber sendCompleted];
            return nil;
        }];
    }];
    // 有没有完成
    [[command executing] subscribeNext:^(NSNumber * _Nullable x) {
        
    }];
    
    RACSignal * sig = [command execute:@"执行"];
    [sig subscribeNext:^(id  _Nullable x) {
        
    }];
}
- (void)sigOfSig{
    RACSubject * sigOfSig = [RACSubject subject];
    RACSubject * sig1 = [RACSubject subject];
    RACSubject * sig2 = [RACSubject subject];
    RACSubject * sig3 = [RACSubject subject];
    
    [sigOfSig.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    [sigOfSig sendNext:sig1];
    [sig1 sendNext:@"1 "];
    [sig2 sendNext:@"2 "];
    [sig3 sendNext:@"3 "];
}
- (void)RACCommand{
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"%@",input);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"测试"];
            return nil;
        }];
    }];
    
    //    // executionSignals 信号源：发送信号的信号
    //    [[command executionSignals] subscribeNext:^(RACSignal * x) {
    //        [x subscribeNext:^(id  _Nullable x) {
    //            NSLog(@"%@",x);
    //        }];
    //    }];
    [command.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
    // 执行命令
    [command execute:@"执行命令参数"];
}
- (void)demo1{
    RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        NSLog(@"%@",input);
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [subscriber sendNext:@"执行完命令产生的数据"];
            return nil;
        }];
    }];
    
    // 执行命令
    RACSignal *singl = [command execute:@"执行命令参数"];
    [singl subscribeNext:^(id  _Nullable x) {
        NSLog(@"%@",x);
    }];
}
- (void)RACMulticastConnection{
    // 不管订阅多少次 只会请求一次数据
    // RACMulticastConnection:必须通过信号创建
    //    RACMulticastConnection 链接类
    // 多次订阅信号
    RACSignal * singal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        // 发送网络请求
        NSLog(@"发送请求");
        [subscriber sendNext:@"数据"];
        return nil;
    }];
    
    RACMulticastConnection * connction = [singal publish];
    
    [connction.signal subscribeNext:^(id  _Nullable x) {
        
    }];
    [connction.signal subscribeNext:^(id  _Nullable x) {
        
    }];
    
    [connction connect];
}
- (void)ractuple{
    // 给某个值的某个属性赋值
    RAC(self.titleLabel,text) = self.textField.rac_textSignal;
    // rav obsever KVO
    [RACObserve(self.view, frame) subscribeNext:^(id  _Nullable x) {
        
    }];
    // 包装元祖
    RACTuple * tupel = RACTuplePack(@1,@1);
    NSLog(@"%ld",tupel.count);
    
    //    RACTupleUnpack(<#...#>)
}
- (void)demo2{
    // 请求
    RACSignal * singal1 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        // 发送请求
        
        [subscriber sendNext:@"1来了"];
        return  nil;
    }];
    // 请求
    RACSignal * singal2 = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        // 发送请求
        [subscriber sendNext:@"2来了"];
        return  nil;
    }];
    // 数组存放信号
    [self rac_liftSelector:@selector(updateUI:data2:) withSignalsFromArray:@[singal1,singal2]];
}
- (void)updateUI:(NSString *)tata1 data2:(NSString *)data2{
    NSLog(@"UI%@%@",tata1,data2);
}
- (BBViewModel *)bbViewModel
{
    if (!_bbViewModel) {
        _bbViewModel = [BBViewModel new];
    }
    return _bbViewModel;
}
@end
